﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;
using Wicresoft.EKA.NRemedy;

namespace Wicresoft.EKA.BOSync
{
    public class IncidentInfo
    {
        IDB db = new OracleDBHelper(ConfigurationManager.ConnectionStrings["BOConnection"].ConnectionString);
        private static readonly string intervalDay = ConfigurationManager.AppSettings["IntervalDay"];

        private static readonly ILog logger = LogManager.GetCurrentClassLogger();
        private ARLoginContext context = null;
        private ARProxy<WSEventModel> proxy = null;

        public IncidentInfo(ARLoginContext context)
        {
            this.context = context;
            this.proxy = new ARProxy<WSEventModel>(context);
        }
        public IList<WSEventModel> GetSourceList(string syncdate)
        {
            IList<WSEventModel> sourceList = null;

            try
            {
                sourceList = proxy.GetListEntry("'" + WSEventModel.ModifiedDate_ID + "'>=\"" + syncdate + "\"", new List<uint>()  
{  
WSEventModel.RequestID_ID,
WSEventModel.Submitter_ID,
WSEventModel.CreateDate_ID,
WSEventModel.AssigneeLoginID_ID,
WSEventModel.LastModifiedBy_ID,
WSEventModel.ModifiedDate_ID,
WSEventModel.Status_ID,
WSEventModel.InstanceId_ID,
WSEventModel.StageCondition_ID,
WSEventModel.CurrentStageNumber_ID,
WSEventModel.SubmitterFullName_ID,
WSEventModel.LastModifyByFullName_ID,
WSEventModel.Alert_ID_ID,
WSEventModel.CustomerEmail_ID,
WSEventModel.CustomerDepartment_ID,
WSEventModel.EmployeeNumber_ID,
WSEventModel.ReturnFlag_ID,
WSEventModel.Resolution_ID,
WSEventModel.ClosedByFullName_ID,
WSEventModel.BusynessSystemID_ID,
WSEventModel.BusinessSystemType_ID,
WSEventModel.ResolvedByFullName_ID,
WSEventModel.ResolvedBy_ID,
WSEventModel.ResolvedDate_ID,
WSEventModel.ClosedBy_ID,
WSEventModel.ClosedDate_ID,
WSEventModel.ResolveCode_ID,
WSEventModel.EventSource_ID,
WSEventModel.Category_ID,
WSEventModel.SubCategory_ID,
WSEventModel.CustomerName_ID,
WSEventModel.Description_ID,
WSEventModel.AssignedGroupID_ID,
WSEventModel.PersonID_ID,
WSEventModel.DetailedDecription_ID,
WSEventModel.IncidentNumber_ID,
WSEventModel.Urgency_ID,
WSEventModel.Impact_ID,
WSEventModel.Priority_ID,
WSEventModel.AssignedGroup_ID,
WSEventModel.Assignee_ID,
WSEventModel.CustomerPhone_ID,
});
            }
            catch (Exception ex)
            {
                logger.Error("Get incident source List Error:" + ex);
            }
            return sourceList;
        }

        public IList<TB_INCIDENT_TEMP> ConvertToDestination(IList<WSEventModel> sourceList)
        {
            IList<TB_INCIDENT_TEMP> destinationList = new List<TB_INCIDENT_TEMP>();
            foreach (var item in sourceList)
            {
                try
                {
                    destinationList.Add(new TB_INCIDENT_TEMP
                    {
                        Assigned_Group = item.AssignedGroup,
                        Assigned_Group_Id = item.AssignedGroupID,
                        Assignee = item.Assignee,
                        Assignee_Login_Id = item.AssigneeLoginID,
                        Business_System_Type = item.BusinessSystemType,
                        Busynesssystemid = item.BusynessSystemID,
                        Category = item.Category,
                        SubCategory = item.SubCategory,
                        Closed_By = item.ClosedBy,
                        Closed_Date = item.ClosedDate,
                        Create_Date = item.CreateDate,
                        Customer_Department = item.CustomerDepartment,
                        Customer_Email = item.CustomerEmail,
                        Customer_Name = item.CustomerName,
                        Customer_Phone = item.CustomerPhone,
                        Description = item.Description,
                        Detailed_Decription = item.DetailedDecription,
                        Employee_Number = item.EmployeeNumber,
                        Event_Source = item.EventSource,
                        Incident_Number = item.IncidentNumber,
                        Last_Modified_By = item.LastModifiedBy,
                        Modified_Date = item.ModifiedDate,
                        Priority = item.Priority.ToString(),
                        Resolution = item.Resolution,
                        Resolve_Code = item.ResolveCode,
                        Resolved_By = item.ResolvedBy,
                        Resolved_Date = item.ResolvedDate,
                        Status = item.Status.ToString(),
                        Submitter = item.Submitter,
                        Urgency = item.Urgency.ToString(),
                        Impact = item.Impact.ToString(),
                        ALERT_ID = item.Alert_ID,
                        CLOSEDBYFULLNAME = item.ClosedByFullName,
                        INSTANCEID = item.InstanceId,
                        LASTMODIFYBYFULLNAME = item.LastModifyByFullName,
                        PERSON_ID = item.PersonID,
                        RESOLVEDBYFULLNAME = item.ResolvedByFullName,
                        RETURN_FLAG = item.ReturnFlag,
                        SUBMITTERFULLNAME = item.SubmitterFullName
                    });
                }
                catch (Exception ex)
                {
                    logger.Error("Convert Data to destination Error【" + item.IncidentNumber + "】" + ex);
                }
            }
            return destinationList;
        }

        public void SyncHandler()
        {
            var doubleIntervalDay = double.Parse(intervalDay);
            var syncdate = DateTime.Now.AddDays(-doubleIntervalDay).ToString();
            var listSource = GetSourceList(syncdate);
            var destinationList = ConvertToDestination(listSource);
            IncidentService incidentsercice = new IncidentService();
            incidentsercice.ImportData(destinationList, db);
            logger.Info("Import Data Count【" + destinationList.Count()+ "】");
        }
    }
}
